// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose:  DOM level 3 Implementation Registry utility function definitions..
//   $Id$


#include <stdafx.h>
#include <Common/ImplReg/msw/ImplRegUtils.h>
#include <DOM3ImplReg_i.c>

// Initialise the implementation registry.
HRESULT xport::InitImplReg(IDOMImplementationRegistry **ppImplReg)
{
	HRESULT hr = S_OK;

	if(ppImplReg) {
		if(*ppImplReg)
			hr = S_FALSE;
		else {
			IUnknown *pUnk = 0;

			CoCreateInstance(CLSID_DOMImplementationRegistry, 0, CLSCTX_ALL, IID_IUnknown, reinterpret_cast<void **>(&pUnk));
			CComQIPtr<IDOMImplementationRegistry> pImplReg = pUnk;

			if(pImplReg)
				*ppImplReg = pImplReg.Detach();
			else
				hr = E_POINTER;

			if(pUnk)
				pUnk->Release();
		}
	} else
		hr = E_INVALIDARG;

	return hr;
}

// Get a DOM implementation of the specified feature(s).
HRESULT xport::GetImplementation(const BSTR sFeatures, IDOMImplementation **ppImpl, IDOMImplementationRegistry **ppImplReg)
{
	HRESULT hr = S_OK;

	if(sFeatures && ppImpl && ppImplReg) {
		InitImplReg(ppImplReg);
		if(*ppImplReg)
			hr = (*ppImplReg)->getDOMImplementation(sFeatures, ppImpl);
		else
			hr = E_POINTER;
	} else
		hr = E_INVALIDARG;
	return hr;
}

// Get a DOM implementation list of the specified feature.
HRESULT xport::GetImplementations(const BSTR sFeature, const BSTR sVersion, IDOMImplementationList **ppImplList, IDOMImplementationRegistry **ppImplReg)
{
	HRESULT hr = S_OK;
	if(sFeature && sVersion && ppImplList && ppImplReg) {
		InitImplReg(ppImplReg);
		if(*ppImplReg)
			hr = (*ppImplReg)->getDOMImplementations(sFeature, sVersion, ppImplList);
		else
			hr = E_POINTER;
	} else
		hr = E_INVALIDARG;
	return hr;
}

// Get the first available DOM implementation of the specified feature.
HRESULT xport::GetFirstImplementation(const BSTR sFeature, const BSTR sVersion, IUnknown **ppFeat, IDOMImplementationRegistry **ppImplReg)
{
	HRESULT hr = S_OK;
	if(sFeature && sVersion && ppFeat && ppImplReg) {
		CComPtr<IDOMImplementationList> pImplList;

		hr = GetImplementations(sFeature, sVersion, &pImplList, ppImplReg);

		if(SUCCEEDED(hr)) {
			long nImpls = 0;

			pImplList->get_length(&nImpls);

			if(nImpls > 0) {
				CComPtr<IDOMImplementation> pImpl;

				hr = pImplList->item(0, &pImpl);

				if(SUCCEEDED(hr))
					hr = pImpl->getFeature(sFeature, sVersion, ppFeat);
			} else
				hr = E_NOTIMPL;
		}
	} else
		hr = E_INVALIDARG;
	return hr;
}

// Get the nth available DOM implementation of the specified feature.
HRESULT xport::GetNthImplementation(const BSTR sFeature, const BSTR sVersion, long nIndex, IUnknown **ppFeat, IDOMImplementationRegistry **ppImplReg)
{
	HRESULT hr = S_OK;
	if(sFeature && sVersion && nIndex >= 0 && ppFeat && ppImplReg) {
		CComPtr<IDOMImplementationList> pImplList;

		hr = GetImplementations(sFeature, sVersion, &pImplList, ppImplReg);

		if(SUCCEEDED(hr)) {
			long nImpls = 0;

			pImplList->get_length(&nImpls);

			if(nImpls > 0) {
				CComPtr<IDOMImplementation> pImpl;

				hr = pImplList->item(nIndex, &pImpl);

				if(SUCCEEDED(hr))
					hr = pImpl->getFeature(sFeature, sVersion, ppFeat);
			} else
				hr = E_NOTIMPL;
		}
	} else
		hr = E_INVALIDARG;
	return hr;
}
